package shopping;

import java.util.Scanner;

public class ShoppingCart{
    public static void addProducts(Products[] shoppingCart, Scanner scanner){
        Products goods=new Products();

        System.out.println("请输入添加商品的名称：");
        goods.name=scanner.nextLine();
        System.out.println("请输入添加商品的种类");
        goods.category=scanner.nextLine();
        System.out.println("请输入添加商品的价格");
        goods.price=scanner.nextDouble();
        System.out.println("请输入添加商品的数量");
        goods.quantity=scanner.nextInt();

        for (int i=0;i<shoppingCart.length; i++){
            if(shoppingCart[i]==null) {
                shoppingCart[i]=goods;
                break;
            }
        }
        System.out.println("您的商品"+goods.name+"成功添加进购物车！");
    }
    public static void checkProducts(Products[] shoppingCart){
        System.out.println("---------------购物车商品---------------");
        System.out.println("名称" + "\t\t\t" + "种类" + "\t\t\t" + "价格" + "\t\t\t" + "数量");
        for (int i = 0; i < shoppingCart.length; i++) {
            if (shoppingCart[i] != null) {
                System.out.println(shoppingCart[i].name + "\t\t\t" + shoppingCart[i].category + "\t\t\t" + shoppingCart[i].price + "\t\t\t" + shoppingCart[i].quantity);
            } else {
                break;
            }
        }
    }
    public static void updateProducts(Products[] shoppingCart, Scanner scanner) {

        while (true) {

            System.out.println("请输入您要修改商品数量的编号：");
            int quantity = scanner.nextInt();
            Products goods = getsProducts(shoppingCart,quantity);
            if (goods== null) {
                System.out.println("购物车没有此商品");
            } else {
                System.out.println("请输入" + goods.name + "商品修改的数量：");
                goods.quantity = scanner.nextInt();
                System.out.println("修改完成！");
                checkProducts(shoppingCart);
                break;
            }
        }
    }
    public static void paysProducts(Products[] shoppingCart) {
        checkProducts(shoppingCart);
        double prices=0;
        for(int i=0; i<shoppingCart.length; i++) {
            if(shoppingCart[i]!=null) {
                prices+=shoppingCart[i].price*shoppingCart[i].quantity;
            } else {
                break;
            }
        }
        System.out.println("结算金额："+prices);
    }
    public static Products getsProducts(Products[] shoppingCart, int num) {
        for (int i=0; i<shoppingCart.length; i++) {
            if (shoppingCart[i] != null) {
                if (false) return null;
                else {
                    return shoppingCart[i];
                }
            }
        }
        return null;
    }
}